﻿using System;
using DG.Tweening;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Gp.Scripts.GameLogic {
    public class DoorDynamicObject : TransDynamicObject {
        [Tooltip("门的主体部分")]
        [SerializeField] [LabelText("活动门")] private GameObject doorObject;

        // [SerializeField] private TYPE _type;\
        [Tooltip("目标地点。如果为空则在触发开门时将doorObject失活")]
        [SerializeField] private Transform destinationPos;
        [SerializeField] private int moveSpeed = 5;

        private bool _isOpened = false;

        public override bool IsActive => _isOpened;


        private void Start() {
            if (!Application.isPlaying) return;
            
            var info = Model.GetInfoData(SaveKey);
            if (info == null) return;


            if (!info.isActive || !doorObject) return;
            
            // 如果目标点不存在，则将门失活
            if (!destinationPos) {
                doorObject.SetActive(false);
                return;
            }
            doorObject.transform.position = destinationPos.transform.position;
        }


        [Button]
        public void ExecuteMoveDoor() {
            if (_isOpened || doorObject == null) return;
            _isOpened = true;
            
            // 如果目标点不存在，则将门失活
            if (!destinationPos) {
                doorObject.SetActive(false);
                return;
            }

            var destination = destinationPos.transform.position;
            var distance = Vector3.Distance(destination, doorObject.transform.position);

            doorObject.transform.DOMove(destination, moveSpeed == 0 ? 0 : distance / moveSpeed);
        }
    }
}